package io.renren.modules.aic.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.aic.entity.MemberMachineEntity;
import io.renren.modules.aic.service.MemberMachineService;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户矿机表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-03-30 15:44:18
 */
@RestController
@RequestMapping("aic/membermachine")
public class MemberMachineController {
	@Autowired
	private MemberMachineService memberMachineService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("aic:membermachine:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = memberMachineService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aic:membermachine:info")
	public R info(@PathVariable("id") Long id) {
		MemberMachineEntity memberMachine = memberMachineService.selectById(id);

		return R.ok().put("memberMachine", memberMachine);
	}

	/**
	 * 启动/暂停矿机
	 */
	@SysLog("启动/暂停会员矿机")
	@RequestMapping("/changeStatus")
	@RequiresPermissions("aic:membermachine:update")
	public R changeStatus(Long[] ids, String type) {
		Assert.notNull(ids, "ids is null");
		Assert.noNullElements(ids, "参数异常[ids]");
		Assert.hasText("type", "参数异常[type]");
		Assert.isTrue("START".equals(type) || "PAUSE".equals(type), "参数[type]超出范围");
		memberMachineService.changeStatus(ids, type);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("删除会员矿机")
	@RequestMapping("/delete")
	@RequiresPermissions("aic:membermachine:delete")
	public R delete(@RequestBody Long[] ids) {
		Assert.notNull(ids, "ids is null");
		Assert.noNullElements(ids, "参数异常[ids]");
		memberMachineService.deleteMemberMachine(ids);
		return R.ok();
	}

}
